<template>
  <el-dialog title="创建广告" :visible.sync="idDetail">
    <el-form ref="ruleForm" :model="form" :rules="rules">
      <!-- <el-form-item label="id" :label-width="formLabelWidth" required>
        <el-input v-model="form.id" autocomplete="off" />
      </el-form-item> -->
      <el-form-item
        label="广告名称"
        prop="advertNo"
        :label-width="formLabelWidth"
        required
      >
        <el-input v-model="form.advertNo" autocomplete="off" placeholder="请输入广告名称" />
      </el-form-item>
      <el-form-item
        label="排序"
        prop="order"
        :label-width="formLabelWidth"
        required
      >
        <el-input v-model="form.order" autocomplete="off" placeholder="值越小优先级越高" />
      </el-form-item>
      <el-form-item
        label="位置"
        prop="advertLocation"
        :label-width="formLabelWidth"
        required
      >
        <el-input v-model="form.advertLocation" autocomplete="off" placeholder="请输入广告位置" />
      </el-form-item>
      <el-form-item label="类型" :label-width="formLabelWidth">
        <!-- <el-input v-model="form.advertType" autocomplete="off" /> -->
        <el-select v-model="form.advertType">
          <el-option
            v-for="item in adverttype"
            :key="item.code"
            :label="item.desc"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="链接地址"
        prop="url"
        :label-width="formLabelWidth"
        required
      >
        <el-input v-model="form.url" autocomplete="off" placeholder="输入格式 www.xxx.com|www.xxx.com" />
      </el-form-item>
      <el-form-item
        label="内容"
        prop="advertContent"
        :label-width="formLabelWidth"
        required
      >
        <el-input v-model="form.advertContent" autocomplete="off" placeholder="请输入广告内容" />
      </el-form-item>
      <!-- <el-form-item label="文件信息id" :label-width="formLabelWidth">
        <el-input v-model="form.fileInfoId" autocomplete="off" />
      </el-form-item> -->
      <el-upload
        class="upload-demo"
        action="http://8.140.56.19:8989/group-wxapp/file/v1"
        :headers="headerObj"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :on-success="handleSuccess"
        multiple
        :limit="5"
        :on-exceed="handleExceed"
        :file-list="fileList"
      >
        <el-button class="btn" size="small" type="primary">点击上传</el-button>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="idDetail = false">取 消</el-button>
      <el-button size="small" @click="resetForm('ruleForm')">重 置</el-button>
      <el-button
        size="small"
        type="primary"
        @click="submit"
      >立即创建</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { advertAdd } from '@/api/advert'
import { getToken } from '@/utils/auth'
export default {
  components: {},
  // eslint-disable-next-line vue/require-prop-types
  props: ['adverttype'],
  data() {
    return {
      idDetail: false,
      formLabelWidth: '15%',
      rules: {
        //   id: [{ required: true, message: '不能为空', trigger: 'blur' }],
        advertNo: [{ required: true, message: '不能为空', trigger: 'blur' }],
        advertLocation: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        advertContent: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        advertType: [{ required: true, message: '不能为空', trigger: 'blur' }],
        url: [{ required: true, message: '不能为空', trigger: 'blur' }],
        order: [{ required: true, message: '不能为空' }]
        // fileInformationResponse: [{ validator: validatefile, trigger: 'blur' }]
      },
      form: {
        id: null,
        advertNo: null,
        advertLocation: null,
        advertContent: null,
        advertType: 0,
        url: null,
        order: null,
        fileInformationResponse: [],
        fileInfoId: []
      },
      fileList: [],
      headerObj: {
        Authorization: 'Bearer ' + getToken(),
        Channel: 'PC'
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 上传图片
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`).then(() => {
        this.form.fileInfoId.splice(this.form.fileInfoId.indexOf(file.response.res.id), 1)
      })
    },
    handleSuccess(res, file) {
      // console.log(res.res.id)
      this.form.fileInfoId.push(res.res.id)
    },
    // 提交创建
    submit() {
      //   console.log(this.$refs)
      this.$refs['ruleForm'].validate((valid) => {
        console.log('ref', valid)
        if (valid) {
          const val = {
            advertNo: this.form.advertNo,
            advertLocation: this.form.advertLocation,
            advertContent: this.form.advertContent,
            advertType: Number(this.form.advertType),
            url: this.form.url,
            order: Number(this.form.order),
            fileInformationResponse: this.form.fileInformationResponse,
            fileInfoId: this.form.fileInfoId
          }
          advertAdd(val)
            .then((res) => {
              this.$message.success('创建成功')
              this.idDetail = false
              this.$emit('sonSay')
            })
            .catch(() => {
              // console.log('ss', err)
              this.$message.error('创建失败')
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }, // 重置
    resetForm(formName) {
      //   console.log(this.$refs[formName].resetFields())
      this.form = {
        id: null,
        advertNo: null,
        advertLocation: null,
        advertContent: null,
        advertType: 0,
        url: null,
        order: null,
        fileInformationResponse: [],
        fileInfoId: []
      }
      this.fileList = []
    }
  }
}
</script>

<style scoped lang="scss">
  .upload-demo .btn{
    margin-left: 30%;
  }
  .el-upload__tip{
    float: right;
    margin-right: 50%;
  }
  .upload-demo{
    overflow: hidden;
  }
</style>
